package com.shopping.shoppingdemo.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.shoppingdemo.config.DataSourceContextHolder;
import com.shopping.shoppingdemo.domain.entity.User;
import com.shopping.shoppingdemo.mapper.UserMapper;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    public User getUserFromRead(Long id) {
        DataSourceContextHolder.set("READ");
        try {
            return this.baseMapper.selectById(id);
        } finally {
            DataSourceContextHolder.clear();
        }
    }

    public void addUserToWrite(User user) {
        DataSourceContextHolder.set("WRITE");
        try {
            this.baseMapper.insert(user);
        } finally {
            DataSourceContextHolder.clear();
        }
    }

    public List<User> getAllFromRead() {
        DataSourceContextHolder.set("READ");
        try {
            return this.baseMapper.selectList(null);
        } finally {
            DataSourceContextHolder.clear();
        }
    }
}